package com.all.ipregion.controller;

import com.all.ipregion.service.SearcherService;
import com.all.ipregion.utils.IpUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LY
 * @create 2024/4/28 16:38
 * @Description ip归属地解析控制层
 */
@RestController
public class RegionController {

    @Resource
    private SearcherService searcherService;

    /**
     * 通过请求获取归属地
     * @param request
     * @return
     */
    @PostMapping("/getRegion")
    public String getRegion(HttpServletRequest request){
        String ipAddr = IpUtils.getIpAddress(request);
        String region = searcherService.getRegion(ipAddr);
        return region;
    }

    /**
     * 通过ip获取归属地
     * @param ipAddr ip地址
     * @return
     */
    @PostMapping("/getRegion2")
    public String getRegion2(@RequestParam String ipAddr){
        String region = searcherService.getRegion(ipAddr.trim());
        return region;
    }
}
